const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nyinit: 0,
    orderStr: 'desc',
    istype: 1,
    id: '',
    date: '',
    dateNo: "",
    orderData: [],
    treatment: []
  },

  fromaTime(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '年' + month + '月'
    } else {
      return year + '年'
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  fromaTimes(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '-' + (month < 10 ? ('0' + month) : month)
    } else {
      return year + ''
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  bindDateChange: function (e) {
    let str = e.detail.value
    if (str.indexOf('-') == -1) {
      console.log(str.indexOf('-'));
      str += '年'
    } else {
      str = str.replace(/-/g, '年') + '月'
    }
    console.log(e, 'picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: str,
      dateNo: e.detail.value
    })
    this.getList()
    this.gettreatment()
  },
  orderStrHeld() {
    if (this.data.orderStr == 'desc') {
      this.setData({
        orderStr: 'asc',
      })
    } else {
      this.setData({
        orderStr: 'desc',
      })
    }
    this.getList()
  },
  tabNY() {
    if (this.data.nyinit == 0) {
      this.setData({
        nyinit: 1,
      })
    } else {
      this.setData({
        nyinit: 0,
      })
    }
    this.setData({
      date: this.fromaTime(new Date()),
      dateNo: this.fromaTimes(new Date())
    })
    this.getList()
    this.gettreatment()
  },
  getList() {
    let that = this
    let newData = {}
    wx.showLoading({
      title: '',
      mask: true
    })
    newData.orderStr = that.data.orderStr
    if (that.data.nyinit == 1) {
      newData.year = that.data.dateNo
    } else {
      newData.yearAndMonth = that.data.dateNo
    }
    wx.request({
      url: app.globalData.serverUrlgdb + '/statistics/count/order/rank',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: newData,
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          that.setData({
            orderData: resData.data.orderRankList
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
        }
      })
    })
  },
  gettreatment() {
    let that = this
    let newData = {}
    newData.orderStr = that.data.orderStr
    if (that.data.nyinit == 1) {
      newData.year = that.data.dateNo
    } else {
      newData.yearAndMonth = that.data.dateNo
    }
    wx.request({
      url: app.globalData.serverUrlgdb + '/statistics/count/treatment/rank',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: newData,
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          that.setData({
            treatment: resData.data.treatmentRankList
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      date: this.fromaTime(new Date()),
      dateNo: this.fromaTimes(new Date())
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
    this.gettreatment()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      date: this.fromaTime(new Date()),
      dateNo: this.fromaTimes(new Date())
    })
    this.getList()
    this.gettreatment()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})